package cn.jg.web.controller.sys.rest;

import cn.jg.framework.web.controller.BaseController;
import cn.jg.framework.web.controller.response.BaseResponse;
import cn.jg.model.sys.User;
import cn.jg.service.sys.UserService;
import cn.jg.web.controller.sys.request.UserCreateRequest;
import cn.jg.web.controller.sys.request.UserUpdateRequest;
import cn.jg.web.controller.sys.response.UserFoundResponse;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Optional;

/**
 * Created by admin on 2023/06/06.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/users")
public class UserController extends BaseController{

    private final UserService userService;

    /**
     * 获取详情
     *
     * @param id ID
     * @return {@link UserFoundResponse}
     */
    @RequiresAuthentication
    @GetMapping(value = "/{id:^[1-9]\\d*$}", produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<UserFoundResponse>> findById(@PathVariable(value = "id") final int id) {
        Optional<User> possible = userService.findById(id);
        if (!possible.isPresent()) {
            return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NOT_FOUND);
        }

        UserFoundResponse response = new UserFoundResponse().convert(possible.get());
        return new ResponseEntity<>(new BaseResponse<>(response), foundHttpStatus(response));
    }

    /**
     * 获取列表
     *
     * @param pageNum 页数
     * @param pageSize 页数大小
     * @return {@link UserFoundResponse}
     */
    @RequiresAuthentication
    @GetMapping(produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<List<UserFoundResponse>>> findAll(
        @RequestParam(value = "pageNum", required = false, defaultValue = "1") @Min(1) final int pageNum,
        @RequestParam(value = "pageSize", required = false, defaultValue = "20") @Min(1) @Max(50) final int pageSize) {
        List<User> result = userService.findAll(pageNum, pageSize);
        List<UserFoundResponse> response = new UserFoundResponse().convertToList(result);
        return new ResponseEntity<>(new BaseResponse<>(response), foundHttpStatus(response));
    }


    /**
     * 保存
     *
     * @param userCreateRequest {@link UserCreateRequest}
     * @return {@link UserFoundResponse}
     */
    @RequiresAuthentication
    @PostMapping(consumes = APPLICATION_JSON, produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<UserFoundResponse>> save(@Valid @RequestBody final UserCreateRequest userCreateRequest) {
        User result = userService.save(userCreateRequest.convert(userCreateRequest));
        UserFoundResponse response = new UserFoundResponse().convert(result);
        return new ResponseEntity<>(new BaseResponse<>(response), HttpStatus.CREATED);
    }

    /**
     * 更新
     *
     * @param id ID
     * @param userUpdateRequest {@link UserUpdateRequest}
     * @return Void
     */
    @RequiresAuthentication
    @PutMapping(value = "/{id:^[1-9]\\d*$}", consumes = APPLICATION_JSON, produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<Void>> update(@PathVariable(value = "id") final int id,
        @Valid @RequestBody UserUpdateRequest userUpdateRequest) {
        if (!userService.exists(id)) {
            return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NOT_FOUND);
        }
        User user = userUpdateRequest.convert(userUpdateRequest);
        user.setId(id);
        userService.update(user);
        return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NO_CONTENT);
    }

    /**
    * 删除
    *
    * @param id ID
    * @return Void
    */
    @RequiresAuthentication
    @DeleteMapping(value = "/{id:^[1-9]\\d*$}", produces = APPLICATION_JSON)
    public ResponseEntity<BaseResponse<Void>> deleteById(@PathVariable(value = "id") final int id) {
        if (!userService.exists(id)) {
        return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NOT_FOUND);
        }
        userService.delete(id);
        return new ResponseEntity<>(new BaseResponse<>(null), HttpStatus.NO_CONTENT);
    }
}
